﻿Public Class frmUsuariosLista

    Public Sub Cajero()
        btnAgregar.Enabled = False
        btnEditar.Enabled = False
        btnHabilitar.Enabled = False
        btnInhabilitar.Enabled = False
    End Sub
    Public Sub Admin()
        btnAgregar.Enabled = True
        btnEditar.Enabled = True
        btnHabilitar.Enabled = True
        btnInhabilitar.Enabled = True
    End Sub
    Private Sub frmUsuariosLista_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Me.Visible = False
        'e.Cancel = True
    End Sub

    Private Sub btnRefrescar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefrescar.Click
        Refrescar()
    End Sub

    Sub Refrescar()
        Dim usuariosBS As New Banco.BS.Usuarios
        Me.dgLista.DataSource = usuariosBS.Listar()
    End Sub

    Private Sub btntAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        Dim mantenimiento As New frmUsuariosAgregar
        mantenimiento.ShowDialog()
        Refrescar()
    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Dim id As Integer
        If dgLista.SelectedRows.Count = 1 Then
            Dim dgvRow As DataGridViewRow
            For Each dgvRow In dgLista.SelectedRows
                id = dgvRow.Cells(0).Value
            Next
            Dim mantenimiento As New frmUsuariosEditar
            mantenimiento.Id = id
            mantenimiento.ShowDialog()
            Refrescar()
        Else
            MessageBox.Show("Debe seleccionar una fila")
        End If
    End Sub

    Private Sub btnHabilitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHabilitar.Click
        Dim id As Integer
        If dgLista.SelectedRows.Count = 1 Then
            Dim dgvRow As DataGridViewRow
            For Each dgvRow In dgLista.SelectedRows
                id = dgvRow.Cells(0).Value
            Next
            Dim mantenimiento As New BS.Usuarios
            mantenimiento.Estado(id, 1)
            Refrescar()
        Else
            MessageBox.Show("Debe seleccionar una fila")
        End If
    End Sub

    Private Sub btnInhabilitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInhabilitar.Click
        Dim id As Integer
        If dgLista.SelectedRows.Count = 1 Then
            Dim dgvRow As DataGridViewRow
            For Each dgvRow In dgLista.SelectedRows
                id = dgvRow.Cells(0).Value
            Next
            Dim mantenimiento As New BS.Usuarios
            mantenimiento.Estado(id, 0)
            Refrescar()
        Else
            MessageBox.Show("Debe seleccionar una fila")
        End If
    End Sub

    Private Sub btnVer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVer.Click
        Dim id As Integer
        If dgLista.SelectedRows.Count = 1 Then
            Dim dgvRow As DataGridViewRow
            For Each dgvRow In dgLista.SelectedRows
                id = dgvRow.Cells(0).Value
            Next
            Dim mantenimiento As New frmUsuariosVer
            mantenimiento.Id = id
            mantenimiento.ShowDialog()
        Else
            MessageBox.Show("Debe seleccionar una fila")
        End If
    End Sub

    Private Sub cbFiltrar_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbFiltrar.SelectedIndexChanged
        Dim accion As String = cbFiltrar.SelectedItem
        If accion = "Todos los Usuarios" Then
            Refrescar()
        End If
        If accion = "Usuarios Activos" Then
            Dim usuariosBS As New BS.Usuarios
            Me.dgLista.DataSource = usuariosBS.Filtrar(1, "")
        End If
        If accion = "Usuarios Inactivos" Then
            Dim usuariosBS As New BS.Usuarios
            Me.dgLista.DataSource = usuariosBS.Filtrar(2, "")
        End If
        If accion = "Identificación" Then
            Dim usuariosBS As New BS.Usuarios
            Dim texto As String = InputBox("Ingrese el número de Identificación que desea buscar", "Validación")
            If texto <> Nothing Then
                Me.dgLista.DataSource = usuariosBS.Filtrar(3, texto)
            End If
        End If
        If accion = "Nombre Completo" Then
            Dim usuariosBS As New BS.Usuarios
            Dim texto As String = InputBox("Ingrese el Nombre Completo o parte del mismo que desea buscar", "Validación")
            If texto <> Nothing Then
                Me.dgLista.DataSource = usuariosBS.Filtrar(4, texto)
            End If
        End If
    End Sub

End Class